<!--查询表单组件验证   -->
<template>
  <el-form
    ref="formRef"
    :inline="true"
    :model="searchModel"
    size="small"
    :label-width="labelWidth"
    class="search-bar"
    @submit.native.prevent
  >
    <el-form-item
      v-for="(item, index) in formOptions"
      :key="index"
      :label-width="item.width"
      :label="item.label"
      :prop="item.prop"
    >
      <el-input
        v-if="item.type === 'input'"
        v-model="searchModel[item.prop]"
        :placeholder="item.placeholder"
        clearable
        @clear="handleQuery"
      />
      <el-date-picker
        v-if="item.type === 'date'"
        v-model="searchModel[item.prop]"
        :picker-options="item.pickerOptions ? item.pickerOptions : {}"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd"
      ></el-date-picker>
      <el-select
        v-if="item.type === 'initSelect'"
        v-model="searchModel[item.prop]"
        :placeholder="item.placeholder"
        clearable
        @change="initSelectChange"
      >
        <el-option
          v-for="option in item.options"
          :key="option.value"
          :label="option.label"
          :value="option.value"
        />
      </el-select>
      <el-select
        v-if="item.type === 'select'"
        v-model="searchModel[item.prop]"
        :placeholder="item.placeholder"
        clearable
        @change="selectChange"
      >
        <el-option
          v-for="option in item.options"
          :key="option.value"
          :label="option.label"
          :value="option.value"
        />
      </el-select>
      <el-cascader
        v-if="item.type === 'cascader'"
        v-model="searchModel[item.prop]"
        :placeholder="item.placeholder"
        :options="item.list"
        :props="{ multiple: item.multiple || false, checkStrictly: true }"
        clearable
      />
    </el-form-item>
    <span>
      <el-button
        v-if="btnItems.btnQuery.type"
        type="primary"
        icon="el-icon-search"
        @click="handleQuery"
      >{{ btnItems.btnQuery.name }}</el-button>
      <el-button
        v-if="btnItems.btnRese.type"
        type="default"
        style="margin-right: 20px"
        @click="onReset"
      >{{ btnItems.btnRese.name }}</el-button>
      <el-button
        v-if="btnItems.btnExport.type"
        type="default"
        icon="el-icon-upload2"
        @click="handleExport"
      >{{ btnItems.btnExport.name }}</el-button>
      <el-button v-if="btnItems.btnAdd.type" type="default" @click="handlePush">{{
        btnItems.btnAdd.name
      }}</el-button>
      <el-button v-if="btnItems.btnImpo.type" type="default" @click="handleImpor">{{
        btnItems.btnImpo.name
      }}</el-button>
      <el-button v-if="btnItems.btnDown.type" type="default" @click="handleDown">{{
        btnItems.btnDown.name
      }}</el-button>
    </span>
  </el-form>
</template>

<script>
export default {
  props: {
    labelWidth: {
      type: String,
      default: '60px'
    },
    formOptions: {
      type: Array,
      required: true
    },
    btnItems: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      searchModel: {}
    }
  },
  methods: {
    initSelectChange(val) {
      this.$emit('initSelect-change', val)
    },
    // 下拉框change事件
    selectChange(val) {
      this.$emit('select-change', val)
    },
    // 查询
    handleQuery() {
      this.$emit('handle-query', this.searchModel)
    },
    // 重置
    onReset() {
      this.$refs.formRef.resetFields()
      this.searchModel = {}
      this.$emit('handle-delete')
    },
    // 新增
    handlePush() {
      this.$emit('handle-push')
    },
    // 导出
    handleExport() {
      this.$emit('handle-export', this.searchModel)
    },

    // 导入
    handleImpor() {
      this.$emit('handle-impor')
    },
    // 下载
    handleDown() {
      this.$emit('handle-down')
    }
  }
}
</script>

<style lang="scss" scoped>
.search-bar {
  padding-bottom: 10px;
  width: 100%;
  overflow: hidden;
}
.search-bar >>> .el-form-item {
  margin-bottom: 10px;
}
::v-deep .el-form-item__label {
  //表单label样式
  width: 85px !important;
}
.search-bar >>> .el-range-separator {
  padding: 0;
}
.search-bar .el-button {
  //按钮样式
  padding: 8px 20px !important;
}
::v-deep .el-form-item--small .el-form-item__content,
.el-form-item--small .el-form-item__label {
  width: 150px;
}
</style>
